package com.zhouyu.exampleThread.threadTest;

import com.zhouyu.exampleThread.builder.ThreadPoolBuilder;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @Author zhouyu
 * @Description 使用Callable和FutureTask来创建线程并返回异步执行的结果
 * @date 2020/6/5 16:30
 **/
public class MyCallable implements Callable<Integer> {

    AtomicInteger integer = new AtomicInteger(1);

    @Override
    public Integer call() throws Exception {
        System.out.println("当前线程:" + Thread.currentThread().getName());
        int i = integer.addAndGet(1);
        return i;
    }

    public static void main(String[] args) {
        try {
            for (int i = 0; i < 10; i++) {
                MyCallable myCallable = new MyCallable();
                Future<Integer> submit = ThreadPoolBuilder.builCommonThreadPool().submit(myCallable);
                System.out.println("结果:" + submit.get());
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
    }
}
